import 'dart:convert';

enum Type {
  image,
  text,
  voice,
}

class MSMileStone {
  int? id;
  int month;
  Type type;
  String content;
  String userId;
  MSMileStone(
      {required this.type,
      this.id,
      required this.userId,
      required this.month,
      required this.content});
  factory MSMileStone.fromJson(Map<String, dynamic> map) {
    return MSMileStone(
        id: map["id"],
        type: Type.values[map["type"]],
        userId: map["userId"],
        month: map["month"],
        content: map["content"]);
  }

  Map<String, dynamic> toJson() {
    Map<String, dynamic> result = {};
    result["type"] = type.index;
    result["userId"] = userId;
    result["month"] = month;
    result["content"] = content;
    if (id != null) {
      result["id"] = id;
    }
    return result;
  }

  List<String> get images {
    if (type == Type.image) {
      return (json.decode(content) as List).map((e) => e as String).toList();
    }

    return [];
  }

  String get text {
    if (type == Type.text) {
      return content;
    }

    return "";
  }

  MSVoice? get voice {
    if (type == Type.voice) {
      return MSVoice.fromJson(json.decode(content));
    }

    return null;
  }
}

class MSVoice {
  String path;
  int length;

  MSVoice({required this.length, required this.path});

  factory MSVoice.fromJson(Map<String, dynamic> map) {
    return MSVoice(length: map["length"], path: map["path"]);
  }

  Map<String,dynamic> toJson(){
    return {
      "length":length,
      "path":path,
    };
  }
}
